﻿namespace UnityEditor
{
    using System;
    using System.Collections.Generic;
    using UnityEditorInternal;
    using UnityEngine;

    internal class MecanimUtilities
    {
        internal static bool AreSameAsset(UnityEngine.Object obj1, UnityEngine.Object obj2)
        {
            return (AssetDatabase.GetAssetPath(obj1) == AssetDatabase.GetAssetPath(obj2));
        }

        internal static void DestroyBlendTreeRecursive(BlendTree blendTree)
        {
            for (int i = 0; i < blendTree.GetChildCount(); i++)
            {
                BlendTree motion = blendTree.GetMotion(i) as BlendTree;
                if ((motion != null) && AreSameAsset(blendTree, motion))
                {
                    DestroyBlendTreeRecursive(motion);
                }
            }
            UnityEngine.Object.DestroyImmediate(blendTree, true);
        }

        internal static void DestroyStateMachineRecursive(StateMachine stateMachine)
        {
            for (int i = 0; i < stateMachine.GetStateMachineCount(); i++)
            {
                StateMachine machine = stateMachine.GetStateMachine(i);
                if (AreSameAsset(stateMachine, machine))
                {
                    DestroyStateMachineRecursive(machine);
                }
            }
            for (int j = 0; j < stateMachine.GetStateCount(); j++)
            {
                for (int k = 0; k < stateMachine.GetMotionSetCount(); k++)
                {
                    BlendTree motion = stateMachine.GetState(j).GetMotion(k) as BlendTree;
                    if ((motion != null) && AreSameAsset(stateMachine, motion))
                    {
                        DestroyBlendTreeRecursive(motion);
                    }
                }
            }
            UnityEngine.Object.DestroyImmediate(stateMachine, true);
        }

        public static List<UnityEditorInternal.State> FindSelectionInState(StateMachine stateMachine, ref StateMachine selectedStateMachine)
        {
            List<UnityEditorInternal.State> list = new List<UnityEditorInternal.State>();
            if (Selection.activeObject is Motion)
            {
                Motion activeObject = Selection.activeObject as Motion;
                foreach (UnityEditorInternal.State state in stateMachine.statesRecursive)
                {
                    for (int i = 0; i < stateMachine.GetMotionSetCount(); i++)
                    {
                        if (HasChildMotion(state.GetMotion(i), activeObject))
                        {
                            selectedStateMachine = state.GetStateMachine();
                            list.Add(state);
                        }
                    }
                }
                return list;
            }
            if (Selection.activeObject is StateMachine)
            {
                StateMachine child = Selection.activeObject as StateMachine;
                if (stateMachine.HasStateMachine(child))
                {
                    selectedStateMachine = child;
                }
            }
            return list;
        }

        public static bool HasChildMotion(Motion parent, Motion motion)
        {
            if (parent == motion)
            {
                return true;
            }
            if (parent is BlendTree)
            {
                BlendTree tree = parent as BlendTree;
                int childCount = tree.GetChildCount();
                for (int i = 0; i < childCount; i++)
                {
                    if (HasChildMotion(tree.GetMotion(i), motion))
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        public static bool StateMachineRelativePath(StateMachine parent, StateMachine toFind, ref List<StateMachine> hierarchy)
        {
            hierarchy.Add(parent);
            if (parent == toFind)
            {
                return true;
            }
            for (int i = 0; i < parent.GetStateMachineCount(); i++)
            {
                if (StateMachineRelativePath(parent.GetStateMachine(i), toFind, ref hierarchy))
                {
                    return true;
                }
            }
            hierarchy.Remove(parent);
            return false;
        }
    }
}

